package com.atguigu.tingshu.common.util;

import com.atguigu.tingshu.common.result.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author xiongkunYang
 */
@Slf4j
public class ResponseUtil {

    public static void out(HttpServletResponse response, Result<?> r) {
        ObjectMapper mapper = new ObjectMapper();
        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        try (PrintWriter writer = response.getWriter()) {
            mapper.writeValue(writer, r);
        } catch (IOException e) {
            log.error("写入响应失败", e);
        }
    }
}
